#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author : zaniu(zzaniu@126.com)
# @Time   : 2021/6/29 16:20 
# @Desc   :
# 
#                   _ooOoo_
#                  o8888888o
#                  88" . "88
#                  (| -_- |)
#                  O\  =  /O
#               ____/`---'\____
#             .'  \\|     |//  `.
#            /  \\|||  :  |||//  \
#           /  _||||| -:- |||||-  \
#           |   | \\\  -  /// |   |
#           | \_|  ''\---/''  |   |
#           \  .-\__  `-`  ___/-. /
#         ___`. .'  /--.--\  `. . __
#      ."" '<  `.___\_<|>_/___.'  >'"".
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
#     \  \ `-.   \_ __\ /__ _/   .-` /  /
# =====`-.____`-.___\_____/___.-`____.-'=====
#                   `=---='
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#             佛祖保佑       永无BUG
#
import logging
import time
from jaeger_client import Config


def init_config():
    log_level = logging.DEBUG
    logging.getLogger('').handlers = []
    logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)

    config = Config(
        config={  # usually read from some yaml config
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'local_agent': {
                'reporting_host': '172.18.2.249',
                'reporting_port': '5775',
            },
            'logging': True,
        },
        service_name='shadiao',
        validate=True,
    )
    # this call also sets opentracing.tracer
    return config.initialize_tracer()


def spider(tracer):
    with tracer.start_span('spiderSpan') as span:
        # span.log_kv({'event': 'test message', 'life': 42})
        time.sleep(2)
        with tracer.start_span('baiSpan', child_of=span):
            # span.log_kv({'event': 'down below'})
            time.sleep(1)
        time.sleep(1)
        with tracer.start_span('taobaoSpan', child_of=span):
            # span.log_kv({'event': 'down below'})
            time.sleep(0.5)


if __name__ == "__main__":
    tracer = init_config()
    spider(tracer)
    time.sleep(2)   # yield to IOLoop to flush the spans - https://github.com/jaegertracing/jaeger-client-python/issues/50
    tracer.close()
